<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1
        function Animal(name,age,color){
            this.name = name
            this.age = age
            this.color = color
        }
        // 在原型添加方法
        Animal.prototype.shout = function(){
            console.log('叫');
        }
        Animal.prototype.jump = function(){
            console.log('跳');
        }
        // 修改this指向把Animal的this指向修改为Dog的空对象
        function Dog(name,age,color){
            Animal.call(this,name,age,color)
        }
        // 修改this指向把Animal的this指向修改为Cat的空对象
        function Cat(name,age,color){
            Animal.call(this,name,age,color)
        }
        // 把Dog的原型对象改成Animal的实例对象
        // 再把Dog的原型对象的constructor的属性重新指向Prototype
        Dog.prototype = new Animal()
        Dog.prototype.constructor = Dog

        Cat.prototype = new Animal()
        Cat.prototype.constructor = Cat
        // 实例化构造函数
        const jm = new Dog('金毛',3,'黄色')
        console.log(jm);

        const md = new Cat('美短',1,'黑白渐变')
        console.log(md);


        jm.shout()
        jm.jump()
        md.shout()
        md.jump()
        // 添加eat方法
        Dog.prototype.eat = function(){
            console.log('吃骨头');
        }
        Cat.prototype.eat = function(){
            console.log('吃鱼');
        }
        
        jm.eat()
        md.eat()


        // 2
    //    const arr = [1,2,4,2,3]
       //set 去重
    //    const res = Array.from((new Set(arr)))

    // 2数组去重

       Array.prototype.set = function(){
        return Array.from((new Set(this)))
       }
       const arr = [1,2,4,2,3]
       console.log(arr.set());

    //    3
       function Patient(name,age,gender,height,weight){
        this.name = name
        this.age = age
        this.gender = gender
        this.height = height
        this.weight = weight
       }
       


       const John = new Patient('John',30,'男',175,75)
       John.__proto__.bmi = function (){
       const arr = Object.values(John)
       const h = arr[3]/100
       const w = arr[4]
       console.log((w/(h*h)).toFixed(2));
       }
    //    John的bmi
       John.bmi()

       John.__proto__.maxHeartRate = function(){
        const arr = Object.values(John)
        return 220-arr[1]
       }
       console.log(John.maxHeartRate());

       John.__proto__.targetHeartRate = function(){
       const min = John.maxHeartRate() * 0.5
       const max = John.maxHeartRate() * 0.85

       console.log(`目标心率范围${min}-${max}`);

       }
    // 目标对象的心率范围
       John.targetHeartRate()



    //    4
    function Food(name,price,ingredients){
        this.name = name
        this.price = price
        this.ingredients= ingredients
    }
    // 向食品添加一种原料
    Food.prototype.addingredients = function({ingredients},ylname,weight) {
        const obj = {
            ylname:ylname,
            weight:weight,
        }
        ingredients.push(obj)
    }

    // 向食品删除一种原料
    Food.prototype.removeingredients = function({ingredients},ylname) {
        const res = ingredients.findIndex((item)=>{
            return item.ylname === ylname
        })
        ingredients.splice(res,1)
    }

    // 食品总重量

    Food.prototype.calculateTotalWeight = function({ingredients}) {
        // console.log(ingredients);
       const res = ingredients.reduce((prev,item)=>prev+item.weight,0)
        console.log(res+'g');
    }

    // 字符串表示
    Food.prototype.toString = function(food) {
        console.log(food);
        const arr = food.ingredients.map((item)=>item.ylname)
        const res =  `名称:${food.name} 价格:${food.price} 原料:${arr.join(',')}`
        console.log(res);
    }




    const hbb = new Food('汉堡包',15,[
    {ylname:'面包',weight:20},
    {ylname:'蔬菜',weight:10},
    {ylname:'沙拉酱',weight:5},])
    

    console.log(hbb);
    hbb.addingredients(hbb,'lals',10)
    hbb.removeingredients(hbb,'蔬菜')
    hbb.calculateTotalWeight(hbb)
    hbb.toString(hbb)


   


      
       
       
    // 检测数据类型  typeof 只能检测基本数据类型，检测null的话是Object
    const a = [1,2,3]
    console.log(typeof a);
    // instansof 检测构造函数的实例对象是否

    


    </script>
</body>
</html>